package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.MInventoryProjectMark;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.material.dao.MInventoryProjectMarkDao;
import com.zhongwang.mms.module.material.model.ComplexStorageModel;
import com.zhongwang.mms.module.material.model.MInventoryProjectMarkModel;
import com.zhongwang.mms.module.material.service.MItyToProService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 库存划项目 控制层
 * @Author: China.wsg
 * @Date: 2020/04/21 16:28
 */
@Controller
@RequestMapping("/material/ityToPro")
public class MItyToProController extends BaseController {

    @Autowired
    private MItyToProService ityToProService;
    @Autowired
    private MInventoryProjectMarkDao inventoryProjectMarkDao;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;

    /**
     * 库存划项目标记列表 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"matinv:matwarpro:view"})
    public String page(Model model) {

        model.addAttribute("userName", ShiroUtils.getUser().getName());
        return "material/matWarManage/ItyToPro/list";
    }

    /**
     * 查询库存划项目标记列表
     *
     * @param inventoryProjectMarkModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/markList")
    public JsonResult markList(MInventoryProjectMarkModel inventoryProjectMarkModel) {
        startPage();
        List<MInventoryProjectMarkModel> list = inventoryProjectMarkDao.findMarkListByQuery(inventoryProjectMarkModel);
        return jsonResult(list);
    }

    /**
     * 库存划项目标记导入 页面
     *
     * @return 页面路径
     */
    @GetMapping("/import")
    @RequiresPermissions({"matinv:matwarproexport:add"})
    public String importMark() {

        return "material/matWarManage/ItyToPro/import";
    }

    /**
     * 库存划项目excel导入执行
     *
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/importFile")
    @RequiresPermissions({"matinv:matwarproexport:add"})
    public JsonResult importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        String msg = ityToProService.importFile1(file);
        JsonResult jsonResult;

        if ("导入失败".equals(msg)) {
            jsonResult = error();
        } else {
            jsonResult = success();
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 导出标记导入异常Excel(15天之内)
     *
     * @param response
     */
    @GetMapping("/errorExport")
    public void errorExport(HttpServletResponse response) {
        ityToProService.exportErrorExport(response);
    }

    /**
     * 修改库存划项目标记状态
     *
     * @param inventoryProjectMark 修改信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/updateStatus")
    @RequiresPermissions(value = {"matinv:matwarpro:edit", "matinv:matwarproexec:edit"}, logical = Logical.OR)
    public JsonResult updateStatus(MInventoryProjectMark inventoryProjectMark) {
        return jsonResult(ityToProService.updateStatus(inventoryProjectMark));
    }

    /**
     * 库存划项目批量提交
     *
     * @param arr 标记Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/togetherCommit")
    @RequiresPermissions({"matinv:matwarpro:edit"})
    public JsonResult togetherCommit(@RequestBody List<Integer> arr) {

        ityToProService.togetherCommit(arr);
        return success();
    }

    /**
     * 保存库存划项目标记
     *
     * @param inventoryProjectMark 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"matinv:matwarpro:edit"})
    public JsonResult save(MInventoryProjectMark inventoryProjectMark) {
        JsonResult jsonResult = new JsonResult();
        String msg = ityToProService.save(inventoryProjectMark);
        if (!msg.contains("标记量大于可用量")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 修改库存划项目标记
     *
     * @param inventoryProjectMark 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"matinv:matwarpro:edit"})
    public JsonResult edit(MInventoryProjectMark inventoryProjectMark) {
        JsonResult jsonResult = new JsonResult();
        String msg = ityToProService.edit(inventoryProjectMark);
        if (!msg.contains("标记量大于可用量")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 库存划项目执行 页面
     *
     * @return 页面路径
     */
    @GetMapping("/check")
    @RequiresPermissions({"matinv:matwaprorexec:view"})
    public String check() {

        return "material/matWarManage/ItyToPro/check";
    }

    /**
     * 库存划项目执行（单条）
     *
     * @param ipmId
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkSingle")
    @RequiresPermissions({"matinv:matwarproexec:edit"})
    public JsonResult checkSingle(Integer ipmId) {
        JsonResult jsonResult = new JsonResult();
        String msg = ityToProService.checkSingle(ipmId);
        if (!msg.contains("库存划项目执行失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 库存划项目执行（批量执行）
     *
     * @param arr 明细Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/checkTogether")
    @RequiresPermissions({"matinv:matwarproexec:edit"})
    public JsonResult checkTogether(@RequestBody List<Integer> arr) {

        JsonResult jsonResult = new JsonResult();
        String msg = ityToProService.checkTogether(arr);
        if (!msg.contains("库存划项目执行失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 库存划项目撤销（单条）
     *
     * @param ipmId
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/cancelSingle")
    @RequiresPermissions({"matinv:matwarproexec:edit"})
    public JsonResult cancelSingle(Integer ipmId) {
        JsonResult jsonResult = new JsonResult();
        String msg = ityToProService.cancelSingle(ipmId);
        if (!msg.contains("库存划项目撤销失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 列表 导出
     *
     * @param inventoryProjectMarkModel 查询条件
     * @return list
     */
    @GetMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(HttpServletResponse response, MInventoryProjectMarkModel inventoryProjectMarkModel) {
        ityToProService.exportExcel(response, inventoryProjectMarkModel);
    }

    /**
     * 单条标记页面
     *
     * @return
     */
    @GetMapping("/createPage")
    @RequiresPermissions({"matinv:matwarpro:edit"})
    public String createPage() {
        return "material/matWarManage/ItyToPro/createPage";
    }

    /**
     * 库存划项目库存信息导出 页面
     *
     * @return
     */
    @GetMapping("/exportPage")
    public String export(Model model) {
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
        return "material/matWarManage/ItyToPro/export";
    }

    /**
     * 列表 导出
     *
     * @param complexStorageModel 查询条件
     * @return list
     */
    @GetMapping("/inventoryExport")
    @ResponseBody
    public void inventoryExport(HttpServletResponse response, ComplexStorageModel complexStorageModel) {
        // 执行导出Excel方法
        ityToProService.inventoryExport(response,complexStorageModel);
    }

}
